package org.dreamfly.netshop.actions.pub;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.dreamfly.netshop.entity.GoodsInfo;
import org.dreamfly.netshop.entity.ShopCart;
import org.dreamfly.netshop.entity.Type;
import org.dreamfly.netshop.manage.ClassManager;
import org.dreamfly.netshop.manage.GoodsInfoManager;
import org.dreamfly.netshop.manage.ShopCartManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;

import com.opensymphony.xwork2.ActionSupport;

/**
 * @author <A href="slonghorn@163.com">Shine</A>
 * @version $Revision: 1.1.1.1 $
 * @since 2009.05.20
 */
@Namespace("/pub/goods")
public class GoodsAction extends ActionSupport {

    @Autowired
    private ClassManager classManager;

    @Autowired
    private GoodsInfoManager goodsInfoManager;

    @Autowired
    private ShopCartManager shopCartManager;

    private List<Type> allClass;

    private long id;

    private Page<GoodsInfo> goodsInfo = new Page<GoodsInfo>(12);

    private Page<GoodsInfo> hotGoodsInfo = new Page<GoodsInfo>(4);

    private Page<GoodsInfo> spGoodsInfo = new Page<GoodsInfo>(4);

    private GoodsInfo goodInfo;

    // private ShopCart shopCart;

    public List<Type> getAllClass() {
        return allClass;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public void setGoodInfo(GoodsInfo goodInfo) {
        this.goodInfo = goodInfo;
    }

    public GoodsInfo getGoodInfo() {
        return goodInfo;
    }

    public Page<GoodsInfo> getGoodsInfo() {
        return goodsInfo;
    }

    public Page<GoodsInfo> getHotGoodsInfo() {
        return hotGoodsInfo;
    }

    public Page<GoodsInfo> getSpGoodsInfo() {
        return spGoodsInfo;
    }

    @Override
    public String execute() {
        // ActionContext ctx = ActionContext.getContext();
        // HttpServletRequest request = (HttpServletRequest) ctx.get(ServletActionContext.HTTP_REQUEST);
        // HttpServletResponse response = (HttpServletResponse) ctx.get(ServletActionContext.HTTP_RESPONSE);
        // classManager.delete((long) 21);
        return SUCCESS;
    }

    public String getGoodDetail() {
        // TODO Auto-generated method stub
        allClass = classManager.getAll();
        goodInfo = goodsInfoManager.get(id);
        return "detail";
    }

    public String getGoodsListByClass() {
        HttpServletRequest request = ServletActionContext.getRequest();
        request.setAttribute("class", classManager.get(id).getClassName());
        allClass = classManager.getAll();
        goodsInfo = goodsInfoManager.getGoodsListByClass(id, goodsInfo);
        return "showByClass";
    }

    public String getGoodsListBySupport() {
        allClass = classManager.getAll();
        HttpServletRequest request = ServletActionContext.getRequest();

        String type = request.getParameter("type");
        if ("hot".equals(type)) {
            goodsInfo = goodsInfoManager.getGoodsListByHot(goodsInfo);
            request.setAttribute("support", "热销商品");
        } else if ("dis".equals(type)) {
            goodsInfo = goodsInfoManager.getGoodsListByDiscount(goodsInfo);
            request.setAttribute("support", "特价商品");
        } else if ("jp".equals(type)) {
            goodsInfo = goodsInfoManager.getGoodsListByRefinement(goodsInfo);
            request.setAttribute("support", "精品推荐");
        } else
            return NONE;
        return "showBySupport";
    }

    public String putIntoShopCart() {
        // HttpServletRequest request = ServletActionContext.getRequest();
        // HttpServletResponse response = ServletActionContext.getResponse();
        // ActionContext ctx = ActionContext.getContext();
        // HttpSession session = (HttpSession) ctx.get(ServletActionContext.SESSION);
        // HttpSession session = request.getSession();

        ShopCart shopCart = new ShopCart();

        // shopCart.setMemberId(session.getAttribute(""));
        shopCartManager.addShopCart(shopCart);
        return null;
    }

    private void getAllType() {
        allClass = classManager.getAll();
    }

    public String searchGoods() {
        // HttpServletRequest request = ServletActionContext.getRequest();
        // HttpServletResponse response = ServletActionContext.getResponse();
        // ActionContext ctx = ActionContext.getContext();
        // HttpSession session = (HttpSession) ctx.get(ServletActionContext.SESSION);
        // HttpSession session = request.getSession();
        //
        // ShopCart shopCart = new ShopCart();
        // shopCart.setMemberId(session.getAttribute(""));
        getAllType();
        if (goodInfo == null)
            return "search";
        goodsInfo = goodsInfoManager.search(goodsInfo, goodInfo);
        return "search";
    }
}
